package com.zixi.ai.chat.service.impl;

import com.alibaba.cloud.ai.dashscope.api.DashScopeResponseFormat;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.zixi.ai.ai.service.IAiModelService;
import com.zixi.ai.chat.service.IChatService;
import com.zixi.ai.llm.entity.AiLlmEntity;
import com.zixi.ai.llm.service.IAiLlmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.util.Optional;

/**
 * @author wfg
 */
@Slf4j
@Service
public class ChatServiceImpl implements IChatService {
    @Autowired
    private  IAiModelService aiModelService;
    @Autowired
    private IAiLlmService aiLlmService;
    @Autowired
    @Qualifier("systemPromptTemplate")
    private PromptTemplate systemPromptTemplate;
    @Autowired
    @Qualifier("deepThinkPromptTemplate")
    private PromptTemplate deepThinkPromptTemplate;
    @Autowired
    private SimpleLoggerAdvisor simpleLoggerAdvisor;
    @Autowired
    private MessageChatMemoryAdvisor messageChatMemoryAdvisor;
    @Override
    public Flux<String> chat(String chatId, Long aiLlmId, String prompt) {
        Optional<AiLlmEntity> aiLlm = aiLlmService.findById(aiLlmId);
        ChatModel chatModel;
        if (aiLlm.isEmpty()) {
            log.error("chat model is null");
            return Flux.empty();
        }
        chatModel = aiModelService.buildChatModel(aiLlm.get().getOptions());
        ChatClient chatClient = ChatClient.builder(chatModel)
                .defaultSystem(
                        systemPromptTemplate.getTemplate()
                ).defaultAdvisors(
                        simpleLoggerAdvisor,
                        messageChatMemoryAdvisor
                ).build();
        ChatClient.ChatClientRequestSpec clientRequestSpec = chatClient.prompt()
                .user(prompt)
                .advisors(memoryAdvisor -> memoryAdvisor
                        .param(ChatMemory.CONVERSATION_ID, chatId)
                );
        return clientRequestSpec.stream().content();
    }

    @Override
    public Flux<String> deepThinkingChat(String chatId, Long aiLlmId, String prompt) {
        Optional<AiLlmEntity> aiLlm = aiLlmService.findById(aiLlmId);
        ChatModel chatModel;
        if (aiLlm.isEmpty()) {
            log.error("Deep thinking chat model is null");
            return Flux.empty();
        }
        chatModel = aiModelService.buildChatModel(aiLlm.get().getOptions());
        ChatClient chatClient = ChatClient.builder(chatModel)
                .defaultSystem(
                        systemPromptTemplate.getTemplate()
                ).defaultAdvisors(
                        simpleLoggerAdvisor,
                        messageChatMemoryAdvisor
                ).build();
        ChatClient.ChatClientRequestSpec clientRequestSpec = chatClient.prompt()
                .system(deepThinkPromptTemplate.getTemplate())
                .user(prompt)
                .advisors(memoryAdvisor -> memoryAdvisor
                        .param(ChatMemory.CONVERSATION_ID, chatId)
                );
        return clientRequestSpec.stream().content();
    }
}
